using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock;

namespace CodePlex.SpaceBlock.UI.Controls
{
    public partial class UserMetadataForm : CodePlex.SpaceBlock.UI.Controls.BaseForm
    {
        public UserMetadataForm()
        {
            InitializeComponent();


            this.Text = Properties.Messages.UserMetadata_Title;

            DataGridView grid = new DataGridView();

            grid.AllowUserToAddRows = true;
            grid.AllowUserToDeleteRows = true;

            grid.AllowUserToResizeRows = false;
            grid.AllowUserToResizeColumns = true;

            grid.RowHeadersVisible = true;

            grid.Columns.Clear();


            DataGridViewTextBoxColumn nameColumn = new DataGridViewTextBoxColumn();
            nameColumn.Name = "Name";
            nameColumn.Width = 250;
            grid.Columns.Add(nameColumn);

            DataGridViewTextBoxColumn valueColumn = new DataGridViewTextBoxColumn();
            valueColumn.Name = "Value";
            valueColumn.Width = 250;
            grid.Columns.Add(valueColumn);


            valueColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;


            UIUtils.FillWith(_gridHolder, grid);


        }


        public Metadata Metadata
        {
            get
            {
                DataGridView grid = _gridHolder.Controls[0] as DataGridView;

                Metadata rt = new Metadata();

                foreach(DataGridViewRow row in grid.Rows) {

                    string name = Convert.ToString(row.Cells[0].Value);
                    string value = Convert.ToString(row.Cells[1].Value);

                    if (!string.IsNullOrEmpty(name))
                    {
                        rt.Set(name, value);
                    }
                }

                return rt;

            }

            set
            {
                DataGridView grid = _gridHolder.Controls[0] as DataGridView;

                grid.Rows.Clear();

                if (value == null)
                {
                    grid.Enabled = false;
                }
                else
                {
                    grid.Enabled = true;

                    foreach (string key in value.Keys)
                    {
                        grid.Rows.Add(key, value[key]);
                    }
                }
            }
        }


    }
}

